//
//  StatusCellTopView.swift
//  MicroBlog
//
//  Created by  NorthCity on 15/11/9.
//  Copyright © 2015年  NorthCity. All rights reserved.
//

import UIKit
import SDWebImage
/// 顶部信息视图
class StatusCellTopView: UIView {
    
    /// 微博视图模型
    var viewModel:StatusViewModel?{
        didSet{
            /// 头像
            iconView.sd_setImageWithURL(viewModel?.userProfileUrl,placeholderImage:viewModel?.userDefaultImage)
            /// 昵称
            nameLabel.text = viewModel?.status.user?.screen_name
            /// 会员
            memberIconView.image = viewModel?.userMemberImage
            /// 用户认证图像 认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
            vipIconView.image = viewModel?.vipIconImage
            /// 时间
            timeLabel.text = "刚刚"
            /// 来源
            sourceLabel.text = "来自 北城微博"
        }
    }
    
    // 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载控件
    /// 头像
    private lazy var iconView:UIImageView = UIImageView(imageName: "avatar_default_big")
    /// 昵称
    private lazy var nameLabel:UILabel = UILabel(title: "北城")
    /// 会员图标
    private lazy var memberIconView = UIImageView(imageName: "common_icon_membership_level1")
    /// 认证图标
    private lazy var vipIconView =  UIImageView(imageName: "avatar_vip")
    /// 时间标签
    private lazy var timeLabel = UILabel(title: "刚刚",fontSize: 11,color: UIColor.orangeColor())
    /// 来源标签
    private lazy var sourceLabel = UILabel(title: "北城之恋",fontSize: 11)
    
}

// MARK: - 设置界面
extension StatusCellTopView {
   
    
    private func setupUI() {
        // 1. 添加控件
        let sepView = UIView()
        sepView.backgroundColor = UIColor.purpleColor()
        addSubview(sepView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        // 2. 自动布局
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.height.equalTo(StatusCellMargin)
        }
    
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.width.equalTo(StatusIconWidth)
            make.height.equalTo(StatusIconWidth)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        vipIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        
    }
}